package io.renren.modules.media.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.media.dao.MediaInfoDao;
import io.renren.modules.media.entity.MediaInfoEntity;
import io.renren.modules.media.ffmpeg.GetVideoInfo;
import io.renren.modules.media.model.VideoInfo;
import io.renren.modules.media.service.MediaInfoService;


@Service("mediaInfoService")
public class MediaInfoServiceImpl extends ServiceImpl<MediaInfoDao, MediaInfoEntity> implements MediaInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String jobId = (String)params.get("jobId");
        String fileName = (String)params.get("fileName");

		IPage<MediaInfoEntity> page = this.page(
			new Query<MediaInfoEntity>().getPage(params),
			new QueryWrapper<MediaInfoEntity>()
				.like(StringUtils.isNotBlank(jobId),"job_id", jobId)
				.like(StringUtils.isNotBlank(fileName),"file_name", fileName)
		);

        return new PageUtils(page);
    }

	@Override
	public int saveInfo(String fileName,String filePath,InputStream inputStream) {
		VideoInfo videoInfo = GetVideoInfo.getVideoInfo(inputStream);
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		MediaInfoEntity info = new MediaInfoEntity();
		info.setFileFormat(suffix);
		info.setFilePath(filePath);
		info.setFileName(fileName);
		info.setSource(1);
		info.setDuration((int)videoInfo.getDuration());
		info.setMd5(videoInfo.getMd5());
		info.setVideoCode(videoInfo.getVideoCode());
		info.setAudioCode(videoInfo.getAudioCode());
		info.setSampleRate(videoInfo.getSampleRate());
		info.setFrameRate((int)videoInfo.getFrameRate());
		info.setChannels(videoInfo.getAudioChannel());
		info.setWidth(videoInfo.getWidth());
		info.setHeight(videoInfo.getHeight());
		info.setCreateTime(new Date());
		save(info);
		
		return info.getId();
	}

	@Override
	public int saveInfo(String jobId, String fileName, String filePath, int type, File file) {
		VideoInfo videoInfo = GetVideoInfo.getVideoInfoByFile(file);
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		MediaInfoEntity info = new MediaInfoEntity();
		info.setJobId(jobId);
		info.setFileFormat(suffix);
		info.setFilePath(filePath);
		info.setFileName(fileName);
		info.setSource(2);
		info.setDuration((int)videoInfo.getDuration());
		info.setMd5(videoInfo.getMd5());
		info.setVideoCode(videoInfo.getVideoCode());
		info.setAudioCode(videoInfo.getAudioCode());
		info.setSampleRate(videoInfo.getSampleRate());
		info.setFrameRate((int)videoInfo.getFrameRate());
		info.setChannels(videoInfo.getAudioChannel());
		info.setWidth(videoInfo.getWidth());
		info.setHeight(videoInfo.getHeight());
		info.setType(type);
		info.setCreateTime(new Date());
		save(info);
		
		return info.getId();
	}
	


}